package cn.ciis.ciis.equipment;

import cn.ciis.basic.base.BaseService;
import cn.ciis.basic.model.Equipment;
import cn.hutool.core.util.StrUtil;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;

import java.util.List;

/**
 * 设备信息 管理
 * 描述：
 * @author 冉志林
 */
public class EquipmentAdminService extends BaseService {

    private Equipment dao = new Equipment().dao();

    @Override
    public Model getDao() {
        return dao;
    }

    /**
     * 根据设备code查询设备信息
     * @param code
     * @return
     */
    public Equipment findByCode(String code){
        return dao.template(getTable()+".findByCode", Kv.by("code",code)).findFirst();
    }


    /**
     * 查询Validity 自动长度大于len的传感器
     * @param len
     * @return
     */
    public List<Equipment> findByValidityLen(int len){

        return dao.template(StrUtil.format("{}.findByValidityLen", getTable()),Kv.by("len", len)).find();

    }

    /**"
     * 查询企业下面所有的设备信息
     * @param eId
     * @return
     */
    public List<Equipment> findEquipmentListByEnterprise(String eId){
        return dao.find("SELECT id,`code`,`name` FROM ciis_equipment WHERE eId =? AND isDel=0 AND id NOT IN (SELECT eId FROM ciis_region_equipment WHERE rId IN (SELECT id FROM ciis_region WHERE eId=?))",eId,eId);
    }

    /**
     * 查询所有在线设备
     * @return
     */
    public List<Equipment> findAllOnLine(){
        return dao.template(StrUtil.format("{}.findAllOnLine", getTable())).find();
    }
}